#include <audio/MIDI/MIDIInDeviceWin32.h>
#include <system/Log.h>

#if NE_PLATFORM == NE_PLATFORM_WINDOWS

namespace ne
{
    MIDIInDeviceWin32::MIDIInDeviceWin32(const uint32_t id, const std::string &name)
        : MIDIInDevice(id, name)
    {
        _create();
    }

    bool MIDIInDeviceWin32::open()
    {
        if (isOpen()) return true;

        MIDIINCAPS inCaps;
        for (uint32_t i=0; i<midiInGetNumDevs(); ++i)
        {
            midiInGetDevCaps(i, &inCaps, sizeof(MIDIINCAPS));
            Log::Out("MIDI IN CAPS[%d, %s]", i, inCaps.szPname);
        }
        midiInOpen(&mInDeviceHandle, MIDI_MAPPER, 0, 0, CALLBACK_NULL);
        Log::Out("MIDIInDeviceWin32[0x%x]", mInDeviceHandle);
        midiInStart(mInDeviceHandle);
        mIsOpen = true;

        return true;
    }

    bool MIDIInDeviceWin32::reset()
    {
        midiInReset(mInDeviceHandle);
        return true;
    }

    bool MIDIInDeviceWin32::close()
    {
        if (!isOpen()) return true;

        midiInStop(mInDeviceHandle);
        midiInClose(mInDeviceHandle);
        mIsOpen = false;

        return false;
    }

    bool MIDIInDeviceWin32::receivedEvent()
    {
        return true;
    }

    MIDIInDeviceWin32::~MIDIInDeviceWin32()
    {
        _destroy();
    }

    
    bool MIDIInDeviceWin32::_create()
    {
        return true;
    }

    bool MIDIInDeviceWin32::_destroy()
    {
        return close();
    }
}

#endif
